package com.scorpio.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.scorpio.basic.dto.BannerDTO;
import com.scorpio.basic.dto.HomeListDTO;
import com.scorpio.basic.service.BannerService;
import com.scorpio.basic.service.HomeListService;
import com.scorpio.common.utils.StringUtils;
import com.scorpio.core.controller.BaseController;
import com.scorpio.core.domain.Result;
import com.scorpio.framework.annotation.Log;
import com.scorpio.redis.constant.AppConfigConstant;
import com.scorpio.system.service.ISysConfigService;
import com.scorpio.utils.AppUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/basic")
public class BasicController extends BaseController {

    @Autowired
    private ISysConfigService sysConfigService;

    @Log(title = "首页数据")
    @GetMapping("home")
    public Result home() {
        JSONObject json = new JSONObject();
        String versions = sysConfigService.selectConfigByKey(AppConfigConstant.CONFIG_MINI_APP_VERSIONS);
        String mversions = AppUtils.getUserDeviceInfo().getVersions();
        if (!StringUtils.isEmpty(versions) && !StringUtils.isEmpty(mversions) && !versions.equals(AppUtils.getUserDeviceInfo().getVersions())) {
            json.put("bannerList", new ArrayList<>());
            json.put("homeList", new ArrayList<>());
        }else {
            List<BannerDTO> bannerList = bannerService.list();
            List<HomeListDTO> homeList = homeListService.appList();
            json.put("bannerList", bannerList);
            json.put("homeList", homeList);
        }

        return Result.success(json);
    }

    @Autowired
    private BannerService bannerService;

    @Autowired
    private HomeListService homeListService;


}
